import axios from 'axios';
import { ElMessage } from 'element-plus'
const instance = axios.create({
    baseURL: 'http://api.jqrjq.cn',
    timeout: 5000
});

// 请求拦截器
instance.interceptors.request.use(config => {
    // 在发送请求之前做些什么，例如添加请求头等
    config.headers['Content-Type'] = 'application/json';
    config.headers['token'] = localStorage.getItem('token') ? localStorage.getItem('token') : ''
    // config.headers['token'] = 'eyJhbGciOiJIUzUxMiJ9.eyJleHAiOjE3MDAzODQyMzYsInN1YiI6InpoYW5ncm9uZ3F1YW4iLCJpYXQiOjE2OTc3OTIyMzYxNTZ9.4XpPjSOL6OewRxZ05jjVy3H1fK3meHSWZHmAAR8OwtybSfkjaA91O0yMqnfehfXvngNErl1Hh4ZGFtZw7EfWVg';
    return config;
}, error => {
    // 处理请求错误
    return Promise.reject(error);
});

// 响应拦截器
instance.interceptors.response.use(response => {
    // 对响应数据做些什么，例如处理错误码等
    console.log(response);
    if (!response.data.success) {
        ElMessage.error(response.data.msg)
        return Promise.reject()
    }
    return response.data;
}, error => {
    // 处理响应错误
    ElMessage.error(error.response.data.msg)
    return Promise.reject(error);
});

export default function request(options = {}) {
    const { url, method = 'get', data = {} } = options;

    return instance({
        method,
        url,
        data
    });
}